package com.gmq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gmq.common.CustomExcetion;
import com.gmq.common.R;
import com.gmq.entity.Category;
import com.gmq.entity.Dish;
import com.gmq.entity.Setmeal;
import com.gmq.mapper.CategoryMapper;
import com.gmq.service.CategoryService;
import com.gmq.service.DishService;
import com.gmq.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void removeCategory(long ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Dish::getCategoryId, ids);
        List<Dish> byId = dishService.list(queryWrapper);

        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.like(Setmeal::getCategoryId, ids);
        List<Setmeal> list = setmealService.list(queryWrapper1);
        if (byId.size() > 0 || list.size() > 0) {
            throw new CustomExcetion("该分类下关联了菜品，无法删除");
        }
    }
}
